from rest_framework.views import APIView
from rest_framework.response import Response
from .serializer import BookSerializer, AuthorSerializer, UserLogSerializer, PublishSerializer
from .models import Book, Author, Publish
# IP信息
from app002.models import UserLog


class BookView(APIView):
    def get(self, request):
        book_all = Book.objects.all()
        ser = BookSerializer(instance=book_all, many=True)
        return Response({'code': 100, 'msg': '查看所有成功', 'results': ser.data})

    def post(self, request):
        ser = BookSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '保存成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})


class BookDetailView(APIView):
    def get(self, request, pk):
        book = Book.objects.filter(pk=pk).first()
        ser = BookSerializer(instance=book)
        return Response({'code': 100, 'msg': '查看成功', 'results': ser.data})

    def put(self, request, pk):
        book = Book.objects.filter(pk=pk).first()
        if not book:
            return Response({'code': 102, 'msg': '当前查询书籍不存在'})
        ser = BookSerializer(instance=book, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '修改成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})

    def delete(self, request, pk):
        Book.objects.filter(pk=pk).delete()
        return Response({'code': 100, 'msg': '删除成功'})


class AuthorView(APIView):
    def get(self, request):
        authors = Author.objects.all()
        ser = AuthorSerializer(instance=authors, many=True)
        return Response({'code': 100, 'msg': '查看成功', 'results': ser.data})

    def post(self, request):
        ser = AuthorSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '保存成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})


class AuthorDetailView(APIView):
    def get(self, request, pk):
        author = Author.objects.filter(pk=pk).first()
        ser = AuthorSerializer(instance=author)
        return Response({'code': 100, 'msg': '查看成功', 'results': ser.data})

    def put(self, request, pk):
        author = Author.objects.filter(pk=pk).first()
        if not author:
            return Response({'code': 102, 'msg': '当前查询作者不存在'})
        ser = AuthorSerializer(instance=author, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '修改成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})

    def delete(self, request, pk):
        Author.objects.filter(pk=pk).delete()
        return Response({'code': 100, 'msg': '删除成功'})


class PublishView(APIView):
    def get(self, request):
        publish = Publish.objects.all()
        ser = PublishSerializer(publish, many=True)
        return Response({'code': 100, 'msg': '查看所有成功', 'results': ser.data})

    def post(self, request):
        ser = PublishSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '保存成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})


class PublishDetailView(APIView):
    def get(self, request, pk):
        publish = Publish.objects.filter(pk=pk).first()
        if not publish:
            return Response({'code': 102, 'msg': '当前查询出版社不存在'})
        ser = PublishSerializer(instance=publish)
        return Response({'code': 100, 'msg': '查看成功', 'results': ser.data})

    def put(self, request, pk):
        publish = Publish.objects.filter(pk=pk).first()
        if not publish:
            return Response({'code': 102, 'msg': '当前查询出版社不存在'})
        ser = PublishSerializer(instance=publish, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 100, 'msg': '修改成功', 'results': ser.data})
        else:
            return Response({'code': 101, 'msg': '出现了错误', 'errors': ser.errors})

    def delete(self, request, pk):
        Publish.objects.filter(pk=pk).delete()
        return Response({'code': 100, 'msg': '删除成功'})


class UserLogView(APIView):
    def get(self, request):
        userlog = UserLog.objects.all()
        ser = UserLogSerializer(userlog, many=True)
        ip_dict = {}
        platform_dict = {}
        method_dict = {}
        for log in ser.data:
            if log['ip'] in ip_dict:
                ip_dict[log['ip']] += 1
            else:
                ip_dict[log['ip']] = 1
            if log['browser'] in platform_dict:
                platform_dict[log['browser']] += 1
            else:
                platform_dict[log['browser']] = 1
            if log['method'] in method_dict:
                method_dict[log['method']] += 1
            else:
                method_dict[log['method']] = 1
        return Response([ip_dict, platform_dict, method_dict])
